package controllers;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import models.Operator;
import services.AuthorityService;
import services.BackCountService;
import services.BackInfoService;
import utils.BizConstants;
import utils.Validate;
import vos.BackCountSearchVo;
import vos.BackCountSubSearchVo;
import vos.BackCountVo;
import vos.BackInfoVo;
import vos.Page;
import vos.ReturnCode;
import vos.ReturnListVo;

/**
 * 月末统计
 * 
 * @ClassName: BackCountController 
 * @author: wangsm
 * @date: 2016年6月8日 上午10:22:05
 */
public class BackCountController extends BaseController {
	private static final SimpleDateFormat monthFormater = new SimpleDateFormat("yyyy-MM");
	/**
	 * 跳转到月末统计页面
	 *
	 * @Title: backtotal 
	 * @param menuid
	 * @return: void
	 */
	public static void backcount(String menuid){
		Operator oper=getLoginUser();
		List<String> auths=AuthorityService.getAuthorityByOperAndMenu(menuid,oper.operatorid);
		for(String auth : auths){
			renderArgs.put(auth, auth);
		}
		render();
	}
	/**
	 * 获取未保存的统计信息
	 *
	 * @Title: getCount 
	 * @return: void
	 */
	public static void getCount(int page,int rows, String sort,String order){
		if(page==0){
			page=1;
		}
		if(rows==0){
			rows=BizConstants.pagesize;
		}
		BackCountSearchVo vo=new BackCountSearchVo(page,rows,sort,order);
		long total=BackCountService.count(vo);
		ReturnListVo listVo=new ReturnListVo();
		listVo.setTotal(total);
		List<BackCountVo> backvos=BackCountService.getCount(vo);
		listVo.setRows(backvos);
		renderJSON(listVo);
	}
	/**
	 * 统计当月经销商返款情况
	 *
	 * @Title: countMonth 
	 * @return: void
	 */
	public static void countMonth(){
		if(!checkAuthority("BACKCOUNT_COUNTMONTH")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		//查找该月份是否已经统计过
		Date date=new Date();
		String datestr=monthFormater.format(date);
		Long monLong=BackCountService.countByMonth(datestr);
		if(monLong>0){
			renderFailedJson(ReturnCode.ErrorCode,"该月份已经进行统计");
		}
		//查找该月份是否存在未保存记录
		Long saveCount=BackCountService.countBySave();
		if(saveCount>0){
			renderFailedJson(ReturnCode.ErrorCode,"存在未保存的统计结果");
		}
		Operator opr=getLoginUser();
		boolean isok=BackCountService.countMonth(opr.operatorid);
		if(isok){
			renderFailedJson(ReturnCode.SuccessCode, "统计完成");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode, "统计失败");
		}
	}
	/**
	 * 重新统计
	 *
	 * @Title: reCount 
	 * @return: void
	 */
	public static void reCount(){
		if(!checkAuthority("BACKCOUNT_RECOUNT")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		Long saveCount=BackCountService.countBySave();
		if(saveCount<=0){
			renderFailedJson(ReturnCode.ErrorCode,"不存在未保存的统计结果");
		}
		Operator opr=getLoginUser();
		boolean isok=BackCountService.reCount(opr.operatorid);
		if(isok){
			renderFailedJson(ReturnCode.SuccessCode, "统计完成");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode, "统计失败");
		}
	}
	/**
	 * 删除未保存的记录
	 *
	 * @Title: delCount 
	 * @return: void
	 */
	public static void delCount(){
		if(!checkAuthority("BACKCOUNT_GIVEUP")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		//查找该月份是否存在未保存记录
		Long saveCount=BackCountService.countBySave();
		if(saveCount<=0){
			renderFailedJson(ReturnCode.ErrorCode,"不存在未保存的统计结果");
		}
		boolean isok=BackCountService.delCount();
		if(isok){
			renderFailedJson(ReturnCode.SuccessCode, "放弃完成");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode, "放弃失败");
		}
	}
	/**
	 * 保存未保存的记录
	 *
	 * @Title: delCount 
	 * @return: void
	 */
	public static void saveCount(){
		if(!checkAuthority("BACKCOUNT_SAVE")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		//查找该月份是否存在未保存记录
		Long saveCount=BackCountService.countBySave();
		if(saveCount<=0){
			renderFailedJson(ReturnCode.ErrorCode,"不存在未保存的统计结果");
		}
		boolean isok=BackCountService.saveCount();
		if(isok){
			renderFailedJson(ReturnCode.SuccessCode, "保存完成");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode, "保存失败");
		}
	}
	/**
	 * 手动统计
	 *
	 * @Title: handCount 
	 * @return: void
	 */
	public static void handCount(String cmonth){
		if(!checkAuthority("BACKCOUNT_HANDCOUNT")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		if(StringUtils.isEmpty(cmonth)){
			renderFailedJson(ReturnCode.ErrorCode, "统计月份不能为空");
		}
		//验证月份格式是否正确
		if(!Validate.isMonth(cmonth)){
			renderFailedJson(ReturnCode.ErrorCode, "月份格式不正确");
		}
		//查找该月份是否已经统计过
		Long monLong=BackCountService.countByMonth(cmonth);
		if(monLong>0){
			renderFailedJson(ReturnCode.ErrorCode,"该月份已经进行统计");
		}
		Operator opr=getLoginUser();
		boolean isok=BackCountService.handCount(opr.operatorid,cmonth);
		if(isok){
			renderFailedJson(ReturnCode.SuccessCode, "统计完成");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode, "统计失败");
		}
	}
	/**
	 * 获取子表信息
	 *
	 * @Title: getSub 
	 * @param page 页
	 * @param rows 页数量
	 * @param sort 排序字段
	 * @param order 排序方式
	 * @param dealerid 经销商
	 * @param cmonth 月份
	 * @return: void
	 */
	public static void getSub(int page,int rows, String sort,String order,String shopid,String startdate,String enddate){
		if(page==0){
			page=1;
		}
		if(rows==0){
			rows=BizConstants.pagesize;
		}
		BackCountSubSearchVo vo=new BackCountSubSearchVo(page,rows,sort,order,shopid,startdate,enddate);
		long total=BackCountService.countSub(vo);
		ReturnListVo listVo=new ReturnListVo();
		listVo.setTotal(total);
		List<BackCountVo> backvos=BackCountService.getCountSub(vo);
		listVo.setRows(backvos);
		renderJSON(listVo);
	}
}
